package com.xxz.terminal;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.customview.widget.ViewDragHelper;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

public class MainPageActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);

        drawerLayout = (DrawerLayout) findViewById(R.id.draw1);
        setDrawerLeftEdgeSize(this,drawerLayout,1);

        ViewPager2 viewPager = findViewById(R.id.viewPage2);
        MyPageAdapter pageAdapter = new MyPageAdapter(this);
        viewPager.setAdapter(pageAdapter);

        int[] images = new int[]{

                R.mipmap.diary,

                R.mipmap.read,

                R.mipmap.tree};

        String[] tiles = new String[]{"随记", "句读", "树洞"};
        TabLayout tabLayout = findViewById(R.id.tab_layout);
        new TabLayoutMediator(tabLayout, viewPager,
                (tab,position) -> tab.setIcon(images[position]).setText(tiles[position])
        ).attach();

        //一些菜单当中的事件
        //获取保存好的字段信息
        SharedPreferences sp = getSharedPreferences("userinformation", Context.MODE_PRIVATE);
        String user_id = sp.getString("user_id",null);
        String nickname = sp.getString("nickname",null);
        String motto = sp.getString("motto",null);

        ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String,String>>();
        String a[] = {user_id,null,nickname,motto};
        String b[] = {"账号","修改密码","昵称","座右铭"};

        for(int i = 0;i< a.length;i++) {

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("information",b[i]);
            map.put("xinxi", a[i]);
            listItems.add(map);

        }

        SettingListViewAdapter adapter = new SettingListViewAdapter(this,R.layout.list_item2,listItems);//自定义adapter添加值
        ListView listView = findViewById(R.id.mylistview2);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);//为listView添加点击响应

        Intent intent = getIntent();
        int index = intent.getIntExtra("index",0);
        if(index == 10){
            drawerLayout.openDrawer(Gravity.LEFT);
        }
    }

//    该方法解决DrawerLayout只能从屏幕边缘滑动的问题,来自csdn
    private void setDrawerLeftEdgeSize (Activity activity, DrawerLayout drawerLayout, float displayWidthPercentage) {
        if (activity == null || drawerLayout == null) return;
        try {
            Field leftDraggerField = drawerLayout.getClass().getDeclaredField("mLeftDragger");//通过反射获得DrawerLayout类中mLeftDragger字段
            leftDraggerField.setAccessible(true);//私有属性要允许修改
            ViewDragHelper vdh = (ViewDragHelper) leftDraggerField.get(drawerLayout);//获取ViewDragHelper的实例, 通过ViewDragHelper实例获取mEdgeSize字段
            Field edgeSizeField = vdh.getClass().getDeclaredField("mEdgeSize");//依旧是通过反射获取ViewDragHelper类中mEdgeSize字段, 这个字段控制边缘触发范围
            edgeSizeField.setAccessible(true);//依然是私有属性要允许修改
            int edgeSize = edgeSizeField.getInt(vdh);//这⾥获得mEdgeSize真实值
            Log.d("AAA", "mEdgeSize: "+edgeSize);//这⾥可以打印⼀下看看值是多少
            //Start 获取⼿机屏幕宽度，单位px
            Point point = new Point();
            getWindowManager().getDefaultDisplay().getRealSize(point);
            //End 获取⼿机屏幕
            Log.d("AAA", "point: "+point.x);//依然可以打印⼀下看看值是多少
            edgeSizeField.setInt(vdh, 150);//这⾥设置mEdgeSize的值，Math.max函数取得是最⼤值，也可以⾃⼰指定想要触发的范围值，如: 500,注
            //写到这⾥已经实现了，但是你会发现，如果长按触发范围，侧边栏也会弹出来，⽽且速度不太同步，稳定

            //Start 解决长按会触发侧边栏
            //长按会触发侧边栏是DrawerLayout类的私有类ViewDragCallback中的mPeekRunnable实现导致，我们通过反射把它置空
            Field leftCallbackField = drawerLayout.getClass().getDeclaredField("mLeftCallback");//通过反射拿到私有类ViewDragCallback字段
            leftCallbackField.setAccessible(true);//私有字段设置允许修改
            ViewDragHelper.Callback vdhCallback = (ViewDragHelper.Callback) leftCallbackField.get(drawerLayout);//ViewDragCallback类是私有类，我们返回类型定义成他的
            Field peekRunnableField = vdhCallback.getClass().getDeclaredField("mPeekRunnable");//我们依然是通过反射拿到关键字段，mPeekRunnable
            peekRunnableField.setAccessible(true);
            //定义⼀个空的实现
            Runnable nullRunnable = new Runnable(){
                @Override
                public void run() {
                }
            };
            peekRunnableField.set(vdhCallback, nullRunnable);//给mPeekRunnable字段置空

        } catch (NoSuchFieldException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }
    }

    //新建日记
    public void create1(View btn){

        Intent intent = new Intent(this,DiaryCreateActivity.class);
        startActivity(intent);

    }
    public void user(View btn){
        drawerLayout.openDrawer(Gravity.LEFT);
    }

    //退出登录操作
    public void exit(View btn){
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("请确认是否退出登录").setPositiveButton("是",new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent intent = new Intent(MainPageActivity.this,LoginActivity.class);
                SharedPreferences sp1 = getSharedPreferences("userinformation",MODE_PRIVATE);
                SharedPreferences sp2 = getSharedPreferences("data",MODE_PRIVATE);
                SharedPreferences.Editor editor = sp1.edit();
                editor.putString("password",null);//用户信息表只清空密码，以便在登录界面可直接填充账号再次快速登录
                editor.apply();
                sp2.edit().clear().commit();//数据表全部清空
                startActivity(intent);
                finish();
                Toast.makeText(MainPageActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("否",null);
        builder.create().show();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ListView listView = findViewById(R.id.mylistview2);
        HashMap<String,String> map = (HashMap<String, String>) listView.getItemAtPosition(position);//获得当前list展示的数据，储存在map中，通过key取值
        if(map.get("information").equals("账号")){
            Toast.makeText(this, "无法修改账号", Toast.LENGTH_SHORT).show();
        }
        else {
            Intent Itemclick = new Intent(this, InformationModificationActivity.class);

            String information = map.get("information");//获得左边信息：标题
            String xinxi = map.get("xinxi");//获得右边具体数据

            Itemclick.putExtra("information", information);
            Itemclick.putExtra("xinxi",xinxi);

            startActivity(Itemclick);
        }
    }



}